package com.baseweb.webadmin.controller.app;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.pojo.UserFollow;
import com.baseweb.webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dengsy on 2017/8/21.
 */
@Controller
@RequestMapping(value = "/appApiFollow")
public class FollowAppController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 添加关注
     * */
    @ResponseBody
    @RequestMapping(value = "/insertIntoUserFollow", method = RequestMethod.POST)
    public Map<String,Object> insertIntoUserFollow(@ModelAttribute()UserFollow userFollow){
        if(userFollow.getUserid()!=null && userFollow.getFollowuserid() !=null && userFollow.getType()!= null){
            if(userService.findUserFollowByUserFollowUserId(Integer.valueOf(userFollow.getUserid()),Integer.valueOf(userFollow.getFollowuserid()))!=null){
                return success(userFollow,"已经关注");
            }else{
                if(userService.insertIntoUserFollow(Integer.valueOf(userFollow.getUserid()),Integer.valueOf(userFollow.getFollowuserid()),Integer.valueOf(userFollow.getType()),1) > 0){
                    return success(userFollow,"关注成功");
                }else {
                    return success(userFollow,"关注失败");
                }
            }
        }else {
            return fail("参数错误");
        }
    }

    /**
     * 取消关注
     * */
    @ResponseBody
    @RequestMapping(value = "/deleteUserFollowById", method = RequestMethod.POST)
    public Map<String,Object> deleteUserFollowById(@ModelAttribute()UserFollow userFollow){
        if(userFollow.getUserid()!=null && userFollow.getFollowuserid() !=null ){
            if(userService.deleteUserFollowById(Integer.valueOf(userFollow.getUserid()),Integer.valueOf(userFollow.getFollowuserid())) > 0){
                return success(userFollow,"取消成功");
            }else {
                return success(userFollow,"取消失败");
            }
        }else {
            return fail("参数错误");
        }
    }

    /**
     * 查询商家是否关注
     * */
    @ResponseBody
    @RequestMapping(value = "/findUserFollowByUserFollowUserId", method = RequestMethod.GET)
    public Map<String,Object> findUserFollowByUserFollowUserId(@ModelAttribute()UserFollow userFollow){
        int isValue = 0;
        if(userService.findUserFollowByUserFollowUserId(Integer.valueOf(userFollow.getUserid()),Integer.valueOf(userFollow.getFollowuserid()))!=null){
            isValue = 1;
        }
        return success(isValue,"查询成功");
    }


    /**
     * 查询用户关注的商家
     * */
    @ResponseBody
    @RequestMapping(value = "/findAllUserFollowByTypeOfUser", method = RequestMethod.POST)
    public Map<String,Object> findAllUserFollowByTypeOfUser(@RequestParam("userid") String userId,@RequestParam("page") int page, @RequestParam("pageNum") int pageNum){
        List<UserFollow> list = userService.findAllUserFollowByTypeOfUser(1,Integer.valueOf(userId),(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countUserFollowByTypeOfUser(1,Integer.valueOf(userId));
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum > pageNum){
            pageSum += 1;
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("followList",list);
        map.put("numSum",numSum);
        map.put("pageSum",pageSum);
        return success(map,"查询成功");
    }

    /**
     * 查询用户关注的案例
     * */
    @ResponseBody
    @RequestMapping(value = "/findAllUserFollowByTypeOfExample", method = RequestMethod.POST)
    public Map<String,Object> findAllUserFollowByTypeOfExample(@RequestParam("userid") String userId,@RequestParam("page") int page, @RequestParam("pageNum") int pageNum){
        List<UserFollow> list = userService.findAllUserFollowByTypeOfExample(2,Integer.valueOf(userId),(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countUserFollowByTypeOfExample(2,Integer.valueOf(userId));
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum > pageNum){
            pageSum += 1;
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("followList",list);
        map.put("numSum",numSum);
        map.put("pageSum",pageSum);
        return success(map,"查询成功");
    }
}
